<?php
/**
 * Enum class for all available weekend options
 *
 * @access 	public
 * @author 	Valentin
 * @copyright	ResMania 2009 all rights reserved.
 * @version	1.0
 * @link		http://developer.resmania.com/api
 * @since  	05-2009
 */
class RM_SessionPrices_Weekend {
    // This values indicates what days are a weekend:
    // 2 is a monday,
    // 128 is a sunday
    // That values are also in the database: in rm_SessionPrices_config table row with name = 'weekend_days' in 'metainfo' field
    const fridaySaturday = 96;          //0110000
    const saturdaySunday = 192;         //1100000
    const fridaySaturdaySunday = 224;   //1110000
    const sunday = 128;                 //1000000

    public static $weekendOptions = array(
        self::fridaySaturday => 'WeekendDaysFriSat',
        self::saturdaySunday => 'WeekendDaysSatSu',
        self::fridaySaturdaySunday => 'WeekendDaysFriSatSun',
        self::sunday => 'WeekendDaysSun'
    );

    /**
     * Check is the input day is a weekend
     *
     * @param int $weekendDays Config weekend value
     * @param int $weekendStartHour
     * @param int $weekendEndHour
     * @param int $weekDay weekday number
     * @return bool
     */
    static function isWeekend($weekendDays, $weekendStartHour, $weekendEndHour, RM_Date $hour)
    {
        $dayNumber = $hour->toString('w');
        $weekendStartDay = self::_getWeekendStartDay($weekendDays);
        $weekendEndDay = self::_getWeekendEndDay($weekendDays);
        if ($dayNumber < $weekendStartDay || $dayNumber > $weekendEndDay) {
            return false;
        }

        if ($dayNumber == $weekendStartDay) {
            return ($weekendStartHour <= $hour->toString('H'));
        }
        if ($dayNumber == $weekendEndDay) {
            return ($weekendEndHour >= $hour->toString('H'));
        }

        return true;
    }

    private static function _getWeekendStartDay($weekendDays){
        $days = range(1, 7, 1);
        foreach ($days as $day) {
            $dayPow = pow(2, $day);
            if (($weekendDays & $dayPow) != 0) {
                return $day;
            }
        }
    }

    private static function _getWeekendEndDay($weekendDays){
        $endDay = 1;
        $days = range(1, 7, 1);
        foreach ($days as $day) {
            $dayPow = pow(2, $day);
            if (($weekendDays & $dayPow) != 0) {
                $endDay = $day;
            }
        }
        return $endDay;
    }

}